import java.util.Scanner;

public class ZeroOneBag {
    // 由n个物品，每一个物品有重量和价值，问容量m千克的背包装的最大价值
    // 输入第一行两个数，n，m
    // 第二行为长度n的数组，代表物品的重量
    // 第三行为长度n的数组，代表物品的价值
    public static void main(String[] args) {
//        Scanner sc = new Scanner(System.in);
//        int n = sc.nextInt();
//        int m = sc.nextInt();
//        int[] weight = new int[n];
//        int[] values = new int[n];
//        for (int i = 0; i < n; i++) {
//            weight[i] = sc.nextInt();
//        }
//        for (int i = 0; i < n; i++) {
//            values[i] = sc.nextInt();
//        }
        int[] weight = {2,3,5,5};
        int[] values = {2,4,3,7};
        int m = 10;
        System.out.println(dfs(weight, values, 0, m));
        System.out.println(dp(weight, values, m));
    }

    public static int dfs(int[] weight, int[] values, int idx, int m) {
        if (idx >= weight.length) return 0;
        if (weight[idx] > m) {
            return dfs(weight, values, idx + 1, m);
        } else {
            return Math.max(dfs(weight, values, idx + 1, m), dfs(weight, values, idx + 1, m - weight[idx]) + values[idx]);
        }
    }

    public static int dp(int[] weight, int[] values, int m) {
        int n = weight.length;
        int[][] help = new int[n + 1][m + 1];
        for (int i = 1; i < n + 1; i++) {
            for (int j = 1; j < m + 1; j++) {
                if (j < weight[i -1]) {
                    help[i][j] = help[i - 1][j];
                } else {
                    help[i][j] = Math.max(help[i - 1][j], help[i - 1][j - weight[i -1]] + values[i -1]);
                }
            }
        }
        return help[n][m];
    }
}
